package xyz.wbsite.dbtool.javafx.manger;

import xyz.wbsite.dbtool.javafx.po.Project;
import xyz.wbsite.dbtool.javafx.tool.FileUtil;
import xyz.wbsite.dbtool.javafx.tool.MapperUtil;

import java.io.File;
import java.io.IOException;

public class JsonManager {

    public Project inflate(String json) {
        Project project = MapperUtil.toJava(json, Project.class);
        return project;
    }

    public Project inflate(File json) {
        try {
            return inflate(FileUtil.readFileToString(json));
        } catch (IOException e) {
            e.printStackTrace();
            return new Project();
        }
    }

    public boolean saveAs(File file, Project project) {
        String data = MapperUtil.toJson(project);
        File file_ = new File(file, project.getName() + ".json");
        return FileUtil.writeStringToFile(data, file_, false, false);
    }

    private boolean getBoolean(String str) {
        if (str != null && str.matches("true|1")) {
            return true;
        }
        return false;
    }
}
